<!DOCTYPE html>
<html>
<head>
<title>用户管理</title>
<meta charset="utf-8">
<link rel="stylesheet" th:href="@{/}+'layuiadmin/style/admin.css'" media="all">
<script src="js/day.js"></script>
<link rel="stylesheet" href="js/zTree/css/zTreeStyle/zTreeStyle.css" media="all">
<script type="text/javascript" src="js/zTree/js/jquery.ztree.all.min.js"></script>
<link rel="stylesheet" th:href="@{/}+'js/jquery.mloading/jquery.mloading.css'" media="all">
<script th:src="@{/}+'js/jquery.mloading/jquery.mloading.js'"></script>
</head>
<body>
<div class="layui-fluid" id="app">
  <div class="layui-card">  
    <div class="layui-card-body">
      <div class="layui-row">
    <div class="layui-col-md2">
       <ul id="treeDemo" class="ztree"></ul>
    </div>
    <div class="layui-col-md10">
    <div class="layui-tab layui-tab-brief" lay-filter="userTab">
  	<ul class="layui-tab-title">
	    <li class="layui-this">用户列表</li>
	    <li>附件管理</li>
  	</ul>
  	<div class="layui-tab-content" style="height: 100px;">
	    <div class="layui-tab-item layui-show">
	    	<div class="layui-row layui-col-space5">
		    <div class="layui-row">
		    <div class="layui-inline">
		         <input type="text" id="name" placeholder="用户名称/电话号码" autocomplete="off" class="layui-input">
		    </div>
		    <div class="layui-btn-group">
			    <button type="button" class="layui-btn layui-btn-primary" data-type="query">查询</button>
			    <button type="button" class="layui-btn" data-type="add">增加</button>
			    <button type="button" class="layui-btn" data-type="batchDel">删除</button>
<!--			    <button type="button" class="layui-btn" data-type="moveOrg">更改部门</button>-->
			    <button type="button" class="layui-btn" data-type="resetPassword">重置密码</button>
<!--			    <button type="button" class="layui-btn" data-type="unbindWx">解绑微信</button>-->
<!--			    <button type="button" class="layui-btn" id="importData">导入</button>-->
<!--			    <button class="layui-btn" data-type="excel">导出</button>-->
		  	</div>
		  	</div>
		 	  <div class="layui-row">
		      <table id="user-table" lay-filter="user-table"></table>
		      </div>
      		</div>
	    </div>
	    <div class="layui-tab-item">
	    	<div class="layui-row">
			    <div class="layui-btn-group">
				    <button type="button" class="layui-btn" data-type="addAttachment">上传附件</button>
				    <button type="button" class="layui-btn" data-type="download">下载</button>
				    <button type="button" class="layui-btn" data-type="editAttachment">修改</button>
				    <button type="button" class="layui-btn" data-type="batchDelAttachment">删除</button>
			  	</div>
			  	</div>
			 	<div class="layui-row">
			      <table id="attachment-table" lay-filter="attachment-table"></table>
			    </div>
	    	
	    </div>
  	</div>
</div> 
    </div>
  </div>
    </div>
  </div>
</div>
<form id="downForm" name="downForm" method="post" action="file/downloadByZip" target="success" enctype="multipart/form-data">
	<input type="hidden" id="fid" name="fid" value="">
	<input type="hidden" id="id" name="id" value="">
</form>
<script th:inline="none">
$("#treeDemo").height(top.height-60);
layui.use(['table','form','element','upload'], function(){
  var form = layui.form
  ,element = layui.element
  ,upload = layui.upload
  ,table = layui.table;
  
  var treeSetting = {
		async: {
			enable: true,
			url: "org/getOrgTree",
			autoParam:["id=pid"]
		},
		view:{
			showTitle: true,
			selectedMulti : false
		},callback:{
			onClick: function(event,treeId,node){
				var whr = {orgId: node.id};
				table.reload('user-table',{
					loading:true,
					where: whr
					,page: {
						 curr: 1 
					}
				});				
			}
		}
	};
	$.fn.zTree.init($("#treeDemo"), treeSetting);
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	table.render({
    elem: '#user-table'
    ,url: 'user/getPageList' 
    ,cols: [[
      {type: 'checkbox'}
      ,{field: 'id', width: 150, title: 'ID',hide:true}
      ,{field: 'name', width: 100, title: '用户名',templet:function(d){
      	return "<a class='colum-link' href='javascript:void(0)' onclick='openDetail(this,\""+d.id+"\")'>" + d.name + "</a>";
      }}
      ,{field: 'code', title: '账号',width:120}
      ,{field: 'jobNumber', title: '工号',width:120}
      ,{field: 'mobileno', title: '手机号',width:120}
      ,{field: 'genderTxt', title: '性别',width:90}
      ,{field: 'fullOrgName', title: '所在部门',width:260}
      ,{field: 'staffTypeTxt', title: '员工类别',width:120}
      ,{field: 'roleName', title: '角色'}
    ]]
    ,page: true
    ,limit: 18
    ,height: top.height-145
    ,text: '对不起，加载出现异常！'
    ,done:function(){
    	enableRowClick(table,form,'user-table',"user-table");
    }
  });
  table.render({
	  elem: '#attachment-table'
	  ,url: 'attachment/getPageList' 
	  ,where:{fid:"#"}
  	  ,cols: [[
  		  {type: 'checkbox'}
  		  ,{field: 'id', width: 150, title: 'ID',hide:true}
		  ,{field: 'name', title: '附件名称',width:300}
		  ,{field: 'sizeDesc', title: '文件大小',width:150}
		  ,{field: 'createUserName', title: '上传人',width:190}
		  ,{field: 'createTime', title: '上传时间',width:180}
		  ,{field: 'descs', title: '备注说明'}
	  ]]
  ,page: true
  ,limit: 30
  ,height: top.height-145
  ,text: '对不起，加载出现异常！'
	  ,done:function(){
		  enableRowClick(table,form,'attachment-table',"attachment-table");
	  }
  });
	window.loadGrid = function(tableName){
		  var checkStatus = table.checkStatus(tableName);
	      checkData = checkStatus.data; 
		  table.reload(tableName);
		  selectedRows(checkData,table,form,tableName,tableName);
	  }
	$("#postName").on("click",function(){
		let url = 'rooter?url=base/post/post-win';
		top.layer.open({
	    	type: 2,
	    	title:'选择岗位',
	    	area:['1000px','700px'],
	    	content: url,
	    	btn:['选择','关闭'],
	    	yes:function(index,layero){
	    		var iframeWin = top.window[layero.find('iframe')[0]['name']];
	    	    var post = iframeWin.getData();
	    	    $("#postCode").val(post.code)
	    	    $("#postName").val(post.name)
	    	    top.layer.close(index)
	    	}
	    })    
	})
	var url = 'importUser/uploadFile';
	var progresswin = null;
	var uploadinterval ;
	upload.render({
	    elem: '#importData'
	    ,url: url
	    ,exts:"xlsx"
	    ,before: function(obj){
	  		  progresswin = layer.msg("正在导入，请耐心等候...",{
	  			time:-1
	  		  }) 
	  		  uploadinterval = setInterval(function(){
		  		  $.ajax({
		  			url : 'importUser/getImportInfo',
		  			type: 'POST',
		  			dataType: 'html',
		  			async:true,
		  			success: function(ret){
		  				if(ret==""){
		  					layer.close(progresswin);
		  					clearInterval(uploadinterval);
		  					return false;
		  				}else{
		  					$("#msg").html(ret);
		  				}
		  			}
		  		})
	  			loadGrid("user-table");
	  		 },2000)
	    },done: function(res, index, upload) {
		    layer.alert(res.msg);
		    table.reload("user-table");
		    clearInterval(uploadinterval);
		},progress: function(n){
				
		},error: function(index, upload){
		  layer.alert("文件导入失败!")
		  clearInterval(uploadinterval);
		}
  });
  var action = {
	query:function(){
		var whr = {name:$('#name').val(),postCode:$("#postCode").val()};	 
		table.reload("user-table",{
			where:whr,page:{curr:1}
		}) 
	},
	batchDel: function(){
      var checkStatus = table.checkStatus('user-table')
      ,checkData = checkStatus.data; 
      var ids = [];
	  for(let i=0;i<checkData.length;i++){
		  ids[i] = checkData[i].id;
	  }
      if(checkData.length === 0){
        return layer.msg('请选择要删除的记录');
      }
      layer.confirm('确定删除吗？', function(index) {
    	  $("#app").mLoading({text:"正在处理，请稍候"});
    	  $.ajax({
    		  url:"user/delete",
    		  type:'POST',
    		  dataType:'json',
    		  data:{ids: ids},
    		  success:function(ret){
    			  table.reload('user-table');
    	          layer.msg('删除成功');
    	          $("#app").mLoading("hide");
    		  }
    	  })
          
       })
    }
    ,add: function(){
    	var selectedNodes = treeObj.getSelectedNodes();
    	var orgId = "";
    	if(selectedNodes.length>0){
    		orgId = selectedNodes[0].id;
    	}
    	top.layer.open({
    		type: 2,
    		title:'增加用户',
    		area:['1000px',600+'px'],
    		content: 'rooter?url=system/user/add-user&orgId='+orgId,
    		btn:['保存','关闭'],
    		yes:function(index,layero){
    			var dataForm = top.layer.getChildFrame('form', index);
    			dataForm.find("#submitBtn").click();
    			return false
    		}
    	});    
    } ,moveOrg: function(){
    	var checkStatus = table.checkStatus('user-table')
        ,checkData = checkStatus.data; 
    	if(checkData.length==0){
    		return layer.msg("请选择用户");
    	}
    	layer.open({
    		type: 2,
    		title:'增加用户',
    		area:['1000px',600+'px'],
    		content: 'rooter?url=system/user/change-org&orgId='+checkData[0].id,
    		btn:['更换部门','关闭'],
    		yes:function(index,layero){
    			var dataForm = layer.getChildFrame('form', index);
    			let orgId = dataForm.find(":input[name=nodeOrgId_select_nodeId]").val();
    			if(!orgId){
    				layer.msg("请选择部门");
    				return false;
    			}
    			let paperId = dataForm.find("#paperId").val();
    			if(!paperId){
    				layer.msg("请选择试卷");
    				return false
    			}
    			axios.post("user/changeOrg",{id:checkData[0].id ,orgId:orgId,paperId:paperId}).then(res=>{
    				layer.msg("更换部门成功");
    				table.reload("user-table")
    			})
    			return false
    		}
    	});    
    },resetPassword: function(){
   	     var checkStatus = table.checkStatus('user-table'),checkData = checkStatus.data; 
		 if(checkData.length !== 1){
	         return layer.msg('请选择要重置密码的记录',{icon:0});
	     }
		 layer.confirm("确定重置"+checkData[0].name+"的密码吗？",function(index){
			 $.ajax({
					url:'user/resetPassword',
					data: {id:checkData[0].id},
					type:'POST',
					dataType:'JSON',
					success:function(ret){
						layer.msg("密码重置成功");
						layer.close(index);
					}
			})
		 })
		 
     
     },batchDelAttachment: function(){
	 	      var checkStatus = table.checkStatus('attachment-table')
	 	      ,checkData = checkStatus.data; 
	 	      var ids = [];
	 		  for(let i=0;i<checkData.length;i++){
	 			  ids[i] = checkData[i].id;
	 		  }
	 	      if(checkData.length === 0){
	 	          return layer.msg('请选择要删除的附件');
	 	      }
	 	      layer.confirm('确定删除吗？', function(index) {
	 	    	  $.ajax({
	 	    		  url:"attachment/delete",
	 	    		  type:'POST',
	 	    		  dataType:'json',
	 	    		  data:{ids: ids},
	 	    		  success:function(ret){
	 	    			  table.reload('attachment-table');
	 	    	          layer.msg('删除成功');
	 	    		  }
	 	    	  })
	 	          
	 	       })
	 	 },addAttachment:function(){
	    	var checkStatus = table.checkStatus('user-table')
	         ,checkData = checkStatus.data; 
	    	 if(checkData.length !== 1){
	             return layer.msg('请选择员工记录',{icon:0});
	         }
	    	 top.layer.open({
	    		type: 2,
	    		title:'上传附件',
	    		area:['940px','600px'],
	    		content: 'rooter?url=system/user/add-attachment&userId='+checkData[0].id,
	    		btn:['上传','关闭'],
	    		yes:function(index,layero){
	    			var dataForm = top.layer.getChildFrame('form', index);
	    			dataForm.find("#upload").click();
	    			return false
	    		}
	    	});    
	    },editAttachment: function(){
 	    	 var checkStatus = table.checkStatus('attachment-table')
 	         ,checkData = checkStatus.data; 
 	    	 if(checkData.length !== 1){
 	             return layer.msg('请选择要修改的附件',{icon:0});
 	         }
 	         
 	    	layer.open({
 	    		type: 2,
 	    		title:'修改附件',
 	    		area:['940px','600px'],
 	    		content: 'rooter?url=system/user/add-attachment&id='+checkData[0].id,
 	    		btn:['保存','关闭'],
 	    		yes:function(index,layero){
 	    			var dataForm = layer.getChildFrame('form', index);
 	    			dataForm.find("#submitBtn").click();
 	    			return false
 	    		}
 	    	});    
 	     },download:function(){
 	    	 var checkStatus = table.checkStatus("attachment-table");
 		     checkData = checkStatus.data; 
 		     if(checkData.length==0){
 		    	 return layer.msg("请选择附件")
 		     }
 		    var checkStatus2 = table.checkStatus("user-table");
		     checkData2 = checkStatus2.data; 
		    
		     if(checkData.length==1){
		    	 $("#id").val(checkData[0].id);
		    	 $("#downForm").attr("action","file/download").submit();
		     }else{
		    	 $("#fid").val(checkData2[0].id);
		    	 $("#downForm").attr("action","file/downloadByZip").submit();
		     }
 	     },excel: function(){
 	    	$("#app").mLoading({text:"正在导出，请稍后"});
 			$("#hideForm").attr("action","user/excel").submit();
 			closeMLodding();
 		},requestSyncData: function(){
			axios.post("user/requestSyncData").then(res=>{
				layer.msg("同步完成");
			})
		},unbindWx:function () {
		  var checkStatus = table.checkStatus('user-table'),checkData = checkStatus.data;
		  if(checkData.length !== 1){
			  return layer.msg('请选择要解绑的记录',{icon:0});
		   }
		   axios.post("user/unbindWx?id="+checkData[0].id).then(res=>{
			  layer.msg("解绑成功");
		   })

	 	}
  };
  
  $('.layui-btn').each(function(i,obj){
	  $(this).on('click', function(){
		   var type = $(this).data('type');
		   action[type].call(this);
	  })
  });

  setTimeout(function(){
	treeObj.expandNode(treeObj.getNodesByParam("id","1",null)[0],true,true,true);  
  },500)
  var closeMLodding = function(){
	  $("#app").mLoading("hide");
  }
});
function openDetail(t,id){
	top.layer.open({
    	type: 2,
    	title:'修改用户信息',
    	area:['1000px','600px'],
    	content: 'rooter?url=system/user/add-user&id='+id,
    	btn:['保存','关闭'],
    	yes:function(index,layero){
    		var dataForm = top.layer.getChildFrame('form', index);
    		dataForm.find("#submitBtn").click();
    		return false
    	}
    });    
}
</script>
</body>